﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.tobyfreeman.utils {

    public class Arrays {

        public function Arrays(){
            super();
            throw (new Error("ERROR com.tobyfreeman.utils.Arrays constructor: Class never intended to be instantiated"));
        }
        public static function findPropValue(array:Array, propName:String, propValue){
            var item:Object;
            var len:Number = array.length;
            var i:int;
            while (i < len) {
                item = array[i];
                if (((((!((item == null))) && (item.hasOwnProperty(propName)))) && ((item[propName] == propValue)))){
                    return (item);
                };
                i++;
            };
            return (null);
        }
        public static function filterPropValue(array:Array, propName:String, propValue):Array{
            var item:Object;
            var matches:Array = [];
            var len:Number = array.length;
            var i:int;
            while (i < len) {
                item = array[i];
                if (((((!((item == null))) && (item.hasOwnProperty(propName)))) && ((item[propName] == propValue)))){
                    matches.push(item);
                };
                i++;
            };
            return (matches);
        }
        public static function randomise(array:Array):Array{
            var index:int;
            var source:Array = array.slice();
            var result:Array = [];
            var i:int = array.length;
            while (i > 0) {
                index = Math.floor((i * Math.random()));
                result.push(source.splice(index, 1));
                i--;
            };
            return (result);
        }
        public static function sum(array:Array):Number{
            var item:*;
            var result:Number = 0;
            for each (item in array) {
                result = (result + Number(item));
            };
            return (result);
        }
        public static function subtractArray(array:Array, removeList:Array):Array{
            var resultList:Array = array.slice();
            var itemCount:int = removeList.length;
            var i:int;
            while (i < itemCount) {
                deleteItem(resultList, removeList[i]);
                i++;
            };
            return (resultList);
        }
        public static function stringToArray(the_str:String, delimiter:String, itemType:String):Array{
            var result_array:Array = new Array();
            var item_array:Array = the_str.split(delimiter);
            var itemCount:uint = ((the_str)=="") ? 0 : item_array.length;
            var i:uint;
            while (i < itemCount) {
                result_array.push(Types.stringToType(item_array[i], itemType));
                i++;
            };
            return (result_array);
        }
        public static function deleteItem(array:Array, theItem:Object):Number{
            array.splice(array.indexOf(theItem), 1);
            return (array.length);
        }
        public static function makeUnique(array:Array):Array{
            var item:*;
            var result:Array = [];
            for each (item in array) {
                if (result.indexOf(item) == -1){
                    result.push(item);
                };
            };
            return (result);
        }

    }
}//package com.tobyfreeman.utils 
